/**
 * 物业管理API服务
 * 
 * 严格按照《App端编码规范权威标准声明.md》API调用规范实现：
 * - 统一错误处理：使用try-catch处理API调用异常
 * - 参数验证：确保API调用参数格式正确
 * - 类型安全：使用明确的参数类型
 * - 完整注释：所有方法都有完整的JSDoc注释
 * - 错误消息处理：优先展示后端返回的提示消息
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 * @version 1.0.0
 */

import request from '@/api/request'

/**
 * 获取物业公司列表（用于下拉选择）
 * @returns {Promise<Object>} 返回物业公司列表
 */
export const getPropertyCompanySelectList = async () => {
  try {
    console.log('调用获取物业公司列表API')

    const response = await request.get('/api/property/companies/select-list')

    console.log('获取物业公司列表API响应:', response)

    if (response.success && response.data) {
      return {
        success: true,
        data: response.data,
        message: response.message || '获取成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '获取失败'
      }
    }
  } catch (error) {
    console.error('获取物业公司列表API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

// 数据权限API
export const dataScopeApi = {
  // 获取当前用户数据权限信息
  getDataScopeInfo: () => {
    return request.get('/api/property/data-scope/current-user')
  },

  // 检查用户物业公司权限
  checkPropertyCompanyPermission: (propertyCompanyId) => {
    return request.get(`/api/property/data-scope/check-property-company/${propertyCompanyId}`)
  },

  // 获取用户关联的物业公司信息
  getUserPropertyCompany: () => {
    return request.get('/api/property/data-scope/user-property-company')
  },

  // 获取用户有权限的物业公司列表
  getUserPropertyCompanies: () => {
    return request.get('/api/property/data-scope/property-companies')
  }
}

// 通用API
export const commonApi = {
  /**
   * 获取社区列表
   * @param {Object} queryDTO 查询参数
   * @returns {Promise<Object>} 社区列表
   */
  getCommunityList: async (queryDTO = {}) => {
    try {
      console.log('调用获取社区列表API', queryDTO)
      
      const response = await request.get('/api/region/communities/list', { params: queryDTO })
      
      console.log('获取社区列表API响应:', response)
      
      if (response.success && response.data) {
        return {
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取社区列表API调用失败:', error)
      return {
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取区域树形结构
   * @param {number} parentId 父区域ID，不传则获取顶级区域
   * @returns {Promise<Object>} 区域树形结构
   */
  getRegionTree: async (parentId = null) => {
    try {
      console.log('调用获取区域树API', { parentId })
      
      const params = parentId ? { parentId } : {}
      const response = await request.get('/api/region/tree', params)
      
      console.log('获取区域树API响应:', response)
      
      if (response.success && response.data) {
        return {
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取区域树API调用失败:', error)
      return {
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取当前用户工作区域列表
   * @returns {Promise<Object>} 工作区域列表
   */
  getWorkAreas: async () => {
    try {
      console.log('调用获取工作区域API')
      
      const response = await request.get('/api/region/zone/work-areas')
      
      console.log('获取工作区域API响应:', response)
      
      if (response.success && response.data) {
        return {
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取工作区域API调用失败:', error)
      return {
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

/**
 * 获取当前用户的物业公司信息
 * @returns {Promise<Object>} 返回当前用户的物业公司信息
 */
export const getCurrentUserPropertyCompany = async () => {
  try {
    console.log('调用获取当前用户物业公司API')
    
    const response = await request.get('/api/property/companies/current-user')
    
    console.log('获取当前用户物业公司API响应:', response)
    
    if (response.success && response.data) {
      return {
        success: true,
        data: response.data,
        message: response.message || '获取成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '获取失败'
      }
    }
  } catch (error) {
    console.error('获取当前用户物业公司API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

// 物业费管理API
export const propertyFeeApi = {
  /**
   * 获取物业费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回物业费列表
   */
  getPropertyFeeList: async (params = {}) => {
    try {
      console.log('调用获取物业费列表API', params)
      
      const response = await request.get('/api/property/property-fees', { params })
      
      console.log('获取物业费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取物业费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取物业费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回物业费详情
   */
  getPropertyFeeDetail: async (billId) => {
    try {
      console.log('调用获取物业费详情API', { billId })
      
      const response = await request.get(`/api/property/property-fees/detail/${billId}`)
      
      console.log('获取物业费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取物业费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 燃气费管理API
export const gasFeeApi = {
  /**
   * 获取燃气费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回燃气费列表
   */
  getGasFeeList: async (params = {}) => {
    try {
      console.log('调用获取燃气费列表API', params)
      
      const response = await request.get('/api/property/gas-fees', { params })
      
      console.log('获取燃气费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取燃气费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取燃气费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回燃气费详情
   */
  getGasFeeDetail: async (billId) => {
    try {
      console.log('调用获取燃气费详情API', { billId })
      
      const response = await request.get(`/api/property/gas-fees/${billId}`)
      
      console.log('获取燃气费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取燃气费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 水电费管理API
export const utilityFeeApi = {
  /**
   * 获取水电费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回水电费列表
   */
  getUtilityFeeList: async (params = {}) => {
    try {
      console.log('调用获取水电费列表API', params)
      
      const response = await request.get('/api/property/utility-fees', { params })
      
      console.log('获取水电费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取水电费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取水电费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回水电费详情
   */
  getUtilityFeeDetail: async (billId) => {
    try {
      console.log('调用获取水电费详情API', { billId })
      
      const response = await request.get(`/api/property/utility-fees/${billId}`)
      
      console.log('获取水电费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取水电费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 卫生费管理API
export const sanitationFeeApi = {
  /**
   * 获取卫生费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回卫生费列表
   */
  getSanitationFeeList: async (params = {}) => {
    try {
      console.log('调用获取卫生费列表API', params)
      
      const response = await request.get('/api/property/sanitation-fees', { params })
      
      console.log('获取卫生费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取卫生费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取卫生费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回卫生费详情
   */
  getSanitationFeeDetail: async (billId) => {
    try {
      console.log('调用获取卫生费详情API', { billId })
      
      const response = await request.get(`/api/property/sanitation-fees/${billId}`)
      
      console.log('获取卫生费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取卫生费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 物业费支付API
export const propertyPaymentApi = {
  /**
   * 创建物业费支付订单
   * @param {Object} paymentData 支付数据
   * @returns {Promise<Object>} 返回支付订单信息
   */
  createPaymentOrder: async (paymentData) => {
    try {
      console.log('调用创建物业费支付订单API', paymentData)
      
      const response = await request.post('/api/property/payment/create-order', paymentData)
      
      console.log('创建物业费支付订单API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '创建成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '创建失败'
        }
      }
    } catch (error) {
      console.error('创建物业费支付订单API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 查询支付订单状态
   * @param {string} orderId 订单ID
   * @returns {Promise<Object>} 返回订单状态
   */
  getPaymentOrderStatus: async (orderId) => {
    try {
      console.log('调用查询支付订单状态API', { orderId })
      
      const response = await request.get(`/api/property/payment/order-status/${orderId}`)
      
      console.log('查询支付订单状态API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '查询成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '查询失败'
        }
      }
    } catch (error) {
      console.error('查询支付订单状态API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 确认支付完成
   * @param {string} orderId 订单ID
   * @param {Object} paymentResult 支付结果
   * @returns {Promise<Object>} 返回确认结果
   */
  confirmPayment: async (orderId, paymentResult) => {
    try {
      console.log('调用确认支付完成API', { orderId, paymentResult })
      
      const response = await request.post(`/api/property/payment/confirm/${orderId}`, paymentResult)
      
      console.log('确认支付完成API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '确认成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '确认失败'
        }
      }
    } catch (error) {
      console.error('确认支付完成API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取支付记录列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回支付记录列表
   */
  getPaymentRecords: async (params = {}) => {
    try {
      console.log('调用获取支付记录列表API', params)
      
      const response = await request.get('/api/property/payment/records', { params })
      
      console.log('获取支付记录列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取支付记录列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 业主管理API
export const ownerApi = {
  /**
   * 获取业主列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回业主列表
   */
  getOwnerList: async (params = {}) => {
    try {
      console.log('调用获取业主列表API', params)
      
      const response = await request.get('/api/property/owners', { params })
      
      console.log('获取业主列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取业主列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取业主详情
   * @param {number} ownerId 业主ID
   * @returns {Promise<Object>} 返回业主详情
   */
  getOwnerDetail: async (ownerId) => {
    try {
      console.log('调用获取业主详情API', { ownerId })
      
      const response = await request.get(`/api/property/owners/${ownerId}`)
      
      console.log('获取业主详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取业主详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 创建业主
   * @param {Object} ownerData 业主数据
   * @returns {Promise<Object>} 返回创建结果
   */
  createOwner: async (ownerData) => {
    try {
      console.log('调用创建业主API', ownerData)
      
      const response = await request.post('/api/property/owners', ownerData)
      
      console.log('创建业主API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '创建成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '创建失败'
        }
      }
    } catch (error) {
      console.error('创建业主API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 更新业主信息
   * @param {number} ownerId 业主ID
   * @param {Object} ownerData 业主数据
   * @returns {Promise<Object>} 返回更新结果
   */
  updateOwner: async (ownerId, ownerData) => {
    try {
      console.log('调用更新业主信息API', { ownerId, ownerData })
      
      const response = await request.put(`/api/property/owners/${ownerId}`, ownerData)
      
      console.log('更新业主信息API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '更新成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '更新失败'
        }
      }
    } catch (error) {
      console.error('更新业主信息API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 业主房户关联API
export const ownerHouseholdApi = {
  /**
   * 获取业主房户关联列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回业主房户关联列表
   */
  getOwnerHouseholdList: async (params = {}) => {
    try {
      console.log('调用获取业主房户关联列表API', params)
      
      const response = await request.get('/api/property/owner-households', { params })
      
      console.log('获取业主房户关联列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取业主房户关联列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 创建业主房户关联
   * @param {Object} relationData 关联数据
   * @returns {Promise<Object>} 返回创建结果
   */
  createOwnerHousehold: async (relationData) => {
    try {
      console.log('调用创建业主房户关联API', relationData)
      
      const response = await request.post('/api/property/owner-households', relationData)
      
      console.log('创建业主房户关联API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '创建成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '创建失败'
        }
      }
    } catch (error) {
      console.error('创建业主房户关联API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 更新业主房户关联
   * @param {number} relationId 关联ID
   * @param {Object} relationData 关联数据
   * @returns {Promise<Object>} 返回更新结果
   */
  updateOwnerHousehold: async (relationId, relationData) => {
    try {
      console.log('调用更新业主房户关联API', { relationId, relationData })
      
      const response = await request.put(`/api/property/owner-households/${relationId}`, relationData)
      
      console.log('更新业主房户关联API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '更新成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '更新失败'
        }
      }
    } catch (error) {
      console.error('更新业主房户关联API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 删除业主房户关联
   * @param {number} relationId 关联ID
   * @returns {Promise<Object>} 返回删除结果
   */
  deleteOwnerHousehold: async (relationId) => {
    try {
      console.log('调用删除业主房户关联API', { relationId })
      
      const response = await request.delete(`/api/property/owner-households/${relationId}`)
      
      console.log('删除业主房户关联API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '删除成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '删除失败'
        }
      }
    } catch (error) {
      console.error('删除业主房户关联API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 电费管理API
export const electricityFeeApi = {
  /**
   * 获取电费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回电费列表
   */
  getElectricityFeeList: async (params = {}) => {
    try {
      console.log('调用获取电费列表API', params)
      
      const response = await request.get('/api/property/electricity-fees', { params })
      
      console.log('获取电费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取电费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取电费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回电费详情
   */
  getElectricityFeeDetail: async (billId) => {
    try {
      console.log('调用获取电费详情API', { billId })
      
      const response = await request.get(`/api/property/electricity-fees/${billId}`)
      
      console.log('获取电费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取电费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 水费管理API
export const waterFeeApi = {
  /**
   * 获取水费列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回水费列表
   */
  getWaterFeeList: async (params = {}) => {
    try {
      console.log('调用获取水费列表API', params)
      
      const response = await request.get('/api/property/water-fees', { params })
      
      console.log('获取水费列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取水费列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取水费详情
   * @param {number} billId 账单ID
   * @returns {Promise<Object>} 返回水费详情
   */
  getWaterFeeDetail: async (billId) => {
    try {
      console.log('调用获取水费详情API', { billId })
      
      const response = await request.get(`/api/property/water-fees/${billId}`)
      
      console.log('获取水费详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取水费详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}

// 物业公司管理API
export const propertyCompanyApi = {
  /**
   * 获取物业公司列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 返回物业公司列表
   */
  getPropertyCompanyList: async (params = {}) => {
    try {
      console.log('调用获取物业公司列表API', params)
      
      const response = await request.get('/api/property/companies', { params })
      
      console.log('获取物业公司列表API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取物业公司列表API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 获取物业公司详情
   * @param {number} companyId 公司ID
   * @returns {Promise<Object>} 返回物业公司详情
   */
  getPropertyCompanyDetail: async (companyId) => {
    try {
      console.log('调用获取物业公司详情API', { companyId })
      
      const response = await request.get(`/api/property/companies/${companyId}`)
      
      console.log('获取物业公司详情API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '获取成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '获取失败'
        }
      }
    } catch (error) {
      console.error('获取物业公司详情API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 创建物业公司
   * @param {Object} companyData 公司数据
   * @returns {Promise<Object>} 返回创建结果
   */
  createPropertyCompany: async (companyData) => {
    try {
      console.log('调用创建物业公司API', companyData)
      
      const response = await request.post('/api/property/companies', companyData)
      
      console.log('创建物业公司API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '创建成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '创建失败'
        }
      }
    } catch (error) {
      console.error('创建物业公司API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 更新物业公司信息
   * @param {number} companyId 公司ID
   * @param {Object} companyData 公司数据
   * @returns {Promise<Object>} 返回更新结果
   */
  updatePropertyCompany: async (companyId, companyData) => {
    try {
      console.log('调用更新物业公司信息API', { companyId, companyData })
      
      const response = await request.put(`/api/property/companies/${companyId}`, companyData)
      
      console.log('更新物业公司信息API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '更新成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '更新失败'
        }
      }
    } catch (error) {
      console.error('更新物业公司信息API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  },

  /**
   * 删除物业公司
   * @param {number} companyId 公司ID
   * @returns {Promise<Object>} 返回删除结果
   */
  deletePropertyCompany: async (companyId) => {
    try {
      console.log('调用删除物业公司API', { companyId })
      
      const response = await request.delete(`/api/property/companies/${companyId}`)
      
      console.log('删除物业公司API响应:', response)
      
      if (response.code === 200) {
        return {
          code: 200,
          success: true,
          data: response.data,
          message: response.message || '删除成功'
        }
      } else {
        return {
          code: response.code || 500,
          success: false,
          data: null,
          message: response.message || response.msg || '删除失败'
        }
      }
    } catch (error) {
      console.error('删除物业公司API调用失败:', error)
      return {
        code: 500,
        success: false,
        data: null,
        message: error.message || '网络错误，请重试'
      }
    }
  }
}